package com.allenkerr.web;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.allenkerr.model.Article;
import com.allenkerr.model.Author;
import com.allenkerr.model.Tutorial;
import com.allenkerr.service.ArticleService;
import com.allenkerr.vo.Result;

/**
 * @author 小柯
 * @date 2017/11/10
 */
@RestController
@RequestMapping(value = "/articles", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ArticleController {

  @Autowired
  private ArticleService articleService;

  @PostMapping("")
  public Result init() {
    return articleService.saveArticles(this.initArticles());
  }

  @GetMapping("")
  public Result findArticles(@RequestParam(value = "query", required = false) String queryString) {
    return articleService.findArticles(queryString);
  }

  @GetMapping("/native")
  public Result findArticlesWithNative(@RequestParam(value = "query", required = false) String queryString) {
    return articleService.findArticlesWithNativeSearchQueryBuilder(queryString);
  }

  private List<Article> initArticles() {
    Author author1 = new Author();
    author1.setId(1L);
    author1.setName("蛋蛋");
    author1.setRemark("Java Developer");

    Tutorial tutorial1 = new Tutorial();
    tutorial1.setId(1L);
    tutorial1.setName("elastic search");

    Article article1 = new Article();
    article1.setId(1L);
    article1.setTitle("springboot integreate elasticsearch");
    article1.setAbstracts("springboot integreate elasticsearch is very easy");
    article1.setTutorial(tutorial1);
    article1.setAuthor(author1);
    article1.setContent("elasticsearch based on lucene,"
            + "spring-data-elastichsearch based on elaticsearch"
            + ",this tutorial tell you how to integrete springboot with spring-data-elasticsearch");
    article1.setPostTime(new Date());
    article1.setClickCount(1L);

    //----------------------------------------------------------------------------------------------

    Author author2 = new Author();
    author2.setId(2L);
    author2.setName("蛋蛋");
    author2.setRemark("Java Developer");

    Tutorial tutorial2 = new Tutorial();
    tutorial2.setId(2L);
    tutorial2.setName("springboot");

    Article article2 = new Article();
    article2.setId(2L);
    article2.setTitle("springboot tutorial");
    article2.setAbstracts("springboot is excellent.");
    article2.setTutorial(tutorial2);
    article2.setAuthor(author2);
    article2.setContent("Takes an opinionated view of building production-ready Spring applications. " +
            "Spring Boot favors convention over configuration and is designed to get you up and " +
            "running as quickly as possible.");
    article2.setPostTime(new Date());
    article2.setClickCount(1L);

    //----------------------------------------------------------------------------------------------
    return Arrays.asList(article1, article2);
  }

}
